package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.util.PageResult;
import com.qk.dto.customer.CustomerDto;
import com.qk.entity.Customer;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import com.qk.vo.customer.CustomerVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@SuppressWarnings("all")
@Transactional(rollbackFor = Exception.class)
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Override
    public PageResult<CustomerVo> getCustomerList(CustomerDto customerDto) {
        PageHelper.startPage(customerDto.getPage(), customerDto.getPageSize());
        List<CustomerVo> list = this.baseMapper.getCustomerList(customerDto);
        Page<CustomerVo> rs = (Page<CustomerVo>)  list;
        return PageResult.<CustomerVo>builder()
                .total((int) rs.getTotal())
                .rows(rs)
                .build();

    }

    @Override
    public void addCustomer(Customer customer) {
        log.info("添加客户：{}", customer);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        this.baseMapper.insert(customer);
    }

    @Override
    public CustomerVo getCustomerById(Integer id) {
        Customer customer = this.baseMapper.selectById(id);
        CustomerVo customerVo = BeanUtil.copyProperties(customer, CustomerVo.class);
        return customerVo;
    }

    @Override
    public void updateCustomer(Customer customer) {
        log.info("更新客户：{}", customer);
        customer.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(customer);
    }


}
